import axios from 'axios'
import {BACKEND_ROOT} from '../Constants'
import qs from 'qs'
axios.defaults.timeout = 10000; // 设置请求超时
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8;'
// axios.defaults.baseURL = 'http://10.200.1.2:8080/enterprise'; // 默认基准url
// axios.defaults.baseURL = 'http://221.231.138.86:41085/enterprise';
axios.defaults.baseURL = BACKEND_ROOT;



/**
 * 封装get请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
                params: params
            })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(data), )
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

export function postFormData(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, )
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

export default axios
